package com.phoenix.Ulties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;

import android.R.bool;
import android.os.Environment;
import android.renderscript.ProgramVertexFixedFunction.Constants;
import android.util.Log;

public class FileManagerUlties {
	private final static String t = "FileUtils";
		public static boolean deleteFolder(String path) {
		// not recursive
		if (path != null && storageReady()) {
			File dir = new File(path);
			if (dir.exists() && dir.isDirectory()) {
				File[] files = dir.listFiles();
				for (File file : files) {
					if (file.isDirectory()) {
						Log.i(t, "delete folder " + file.getPath());
						deleteFolder(file.getPath());
					} else {
						Log.i(t, "delete file " + file);
						if (!file.delete()) {
							Log.i(t, "Failed to delete " + file);
						}
					}
				}
			}
			return dir.delete();
		} else {
			return false;
		}
	}
	public static void deleteFileinFolder(String path) {
		// not recursive
		if (path != null && storageReady()) {
			File dir = new File(path);
			if (dir.exists() && dir.isDirectory()) {
				File[] files = dir.listFiles();
				for (File file : files) {
					if (file.isDirectory()) {
						Log.i(t, "delete folder " + file.getPath());
						deleteFolder(file.getPath());
					} else {
						Log.i(t, "delete file " + file);
						if (!file.delete()) {
							Log.i(t, "Failed to delete " + file);
						}
					}
				}
			}
		}
	}

	public static boolean createFolder(String path) {
		if (storageReady()) {
			boolean made = true;
			File dir = new File(path);
			if (!dir.exists()) {
				made = dir.mkdirs();
			}
			return made;
		} else {
			return false;
		}
	}


	public static boolean deleteFile(String path) {
		if (storageReady()) {
			Log.i("delete", "delete file:" +path);
			File f = new File(path);
			return f.delete();
		} else {
			Log.i("delet action", "false");
			return false;
		}
	}
	public static boolean copyFile(File scr, File dest){
		boolean ret = true;
		if (!storageReady()) {
			return false;
		}
		if (scr == null || (scr != null && !scr.exists())|| dest == null){
			return false;
		}
		InputStream in = null;
		OutputStream out = null;
		try{
			in = new FileInputStream(scr);

			//For Overwrite the file.
			out = new FileOutputStream(dest);

			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0){
				out.write(buf, 0, len);
				
			}
			out.flush();
			in.close();
			out.close();
		}
		catch(java.io.FileNotFoundException e){
			e.printStackTrace();
			return false;
		}
		catch(IOException e){
			e.printStackTrace();
			return false;
		}finally{
			if(in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
			if(out != null) {
				try {
					out.close();
				} catch (IOException e) {
				}
			}
		}
		return ret;
	}
	public static boolean isStorageAvailable() {
		String externalStorageState =
			Environment.getExternalStorageState();
		if (!externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
			return false;
		}
		return true;
	}
	public static ArrayList<String> getListFileSDcard()
	{
		File mfile=new File(FileManagerConstants.RootLinksSDCard);
		File[] list=mfile.listFiles();
		ArrayList<String> list_file_sdcard= new ArrayList<String>();
		for(int i=0;i<mfile.listFiles().length;i++)
		{
		    if(!list[i].isHidden())
		    {
		    	list_file_sdcard.add(list[i].getName());
		    }
		}
		return list_file_sdcard;
	}
	public static Boolean isDirectory(String path)
	{
		
		File file = new File(path);
		if (file.isDirectory()) {
		    String[] files = file.list();
		    if (files.length == 0) {
		        //directory is empty
		    }
		    return true;
		}
		else
		return false;
	}
	public static Boolean isDirectoryEmty(String path)
	{
		File file = new File(path);
		    String[] files = file.list();
		    if (files.length == 0) {
		    	//directory is empty
		    	return false;
		    }
		    return true;
	}
	
	public static ArrayList<String> isListFilePath(String path)
	{
		File file = new File(path);
		ArrayList<String> list_file_name = new ArrayList<String>();
		if (file.isDirectory()) {
		    String[] files = file.list();
		    if (files.length != 0) {
		        //directory is empty
		    	for (int j = 0; j < files.length; j++) {
			    	list_file_name.add(files[j]);					
				}
		    }
		}
		return list_file_name;
	}
	
	public static ArrayList<String> sortByName(ArrayList<String> list_name)
	{
		Collections.sort(list_name, String.CASE_INSENSITIVE_ORDER);
		return list_name;
	}
	public static Boolean isImageFile(String path)
	{
		String[] file_tmp = path.split("\\.");
		if (file_tmp.length>1) {
			if ((file_tmp[1].equals("jpg")||(file_tmp[1].equals("png"))||(file_tmp[1].equals("jpg")))) {
				return true;	
				}else	
				return false;			
		}
		else return false;

	}
	public static Boolean isInstallFile(String path)
	{
		String[] file_tmp = path.split("\\.");
		if (file_tmp.length>1) {
			if ((file_tmp[1].equals("apk"))) {
				return true;	
				}else	
				return false;			
		}
		else return false;

	}
	public static void renameFileSDCard(String old_name,String new_name)
	{
		File olde_name_file = new File(old_name);
		if(olde_name_file.exists()){
			File new_name_file = new File(new_name);
//		    File to = new File(dir,new_name);
//		     if(from.exists())
			olde_name_file.renameTo(new_name_file);
		}
	}
	
	private static boolean storageReady() {
		String cardstatus = Environment.getExternalStorageState();
		if (cardstatus.equals(Environment.MEDIA_REMOVED)
				|| cardstatus.equals(Environment.MEDIA_UNMOUNTABLE)
				|| cardstatus.equals(Environment.MEDIA_UNMOUNTED)
				|| cardstatus.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
			return false;
		} else {
			return true;
		}
	}
}
